﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Moq;

namespace MvcWiki.Test.Service.WikiPageService
{
    public class CreateWikiPageFacts
    {
        [Fact]
        public void Returns_WikiPage_By_Namespace_And_Name()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            MvcWiki.Model.WikiPage wikiPage = service.CreateWikiPage(wikiNamespace, "Page");
        }

        [Fact]
        public void Calls_IWikiPageRepository_SaveWikiPage()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();
            wikiPageRepository.Setup(x => x.SaveWikiPage(It.Is<MvcWiki.Model.WikiPage>(y => y.Name == "Page")));

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            MvcWiki.Model.WikiPage wikiPage = service.CreateWikiPage(wikiNamespace, "Page");

            wikiPageRepository.VerifyAll();
        }

        [Fact]
        public void Calls_IUnitOfWork_Commit()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();
            unitOfWork.Setup(x => x.Commit());

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            MvcWiki.Model.WikiPage wikiPage = service.CreateWikiPage(wikiNamespace, "Page");

            unitOfWork.VerifyAll();
        }

        [Fact]
        public void Calls_WikiUserProvider_CanEditNamespace()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            service.CreateWikiPage(wikiNamespace, "Page");

            wikiUserProvider.Verify(x => x.CanEditNamespace(wikiNamespace));
        }

        [Fact]
        public void Throws_Exception_If_CanEditNamespace_Is_False()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(false);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            Assert.Throws<MvcWiki.Exceptions.AuthorizationException>(
                () => service.CreateWikiPage(wikiNamespace, "Page"));
        }

        [Fact]
        public void Calls_WikiUserProvider_CanCreatePage()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(true);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            service.CreateWikiPage(wikiNamespace, "Page");

            wikiUserProvider.Verify(x => x.CanCreatePage());
        }

        [Fact]
        public void Throws_Exception_If_CanCreatePage_Is_False()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace", Markup = new MvcWiki.Model.WikiMarkup() };

            var wikiPageRepository = new Mock<MvcWiki.Repository.IWikiPageRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiPageRepository()).Returns(wikiPageRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiUserProvider = new Mock<MvcWiki.User.IWikiUserProvider>();
            wikiUserProvider.Setup(x => x.CanEditNamespace(wikiNamespace)).Returns(true);
            wikiUserProvider.Setup(x => x.CanCreatePage()).Returns(false);

            var service = new MvcWiki.Service.WikiPageService(repositoryFactory.Object, unitOfWork.Object, wikiUserProvider.Object);

            Assert.Throws<MvcWiki.Exceptions.AuthorizationException>(
                () => service.CreateWikiPage(wikiNamespace, "Page"));
        }
    }
}
